<?php

namespace plugin\cms\app\controller\admin;

use core\controller\AdminBase;
use Exception;
use plugin\cms\app\bll\Tag as BllTag;
use support\View;

class Tag extends AdminBase
{
    public function index()
    {
        $bllTag = new BllTag();
        $tags = $bllTag->paginate(siteid:request()->nowsiteid);
        View::assign('tags', $tags);
        return $this->view();
    }

    public function add()
    {
        if (is_post()) {
            try {
                $data = request()->post();
                $data['siteid'] = request()->nowsiteid;
                $bllTag = new BllTag();
                $bllTag->add($data);
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            return $this->view();
        }
    }

    public function edit()
    {
        $id = request()->get('id');
        $bllTag = new BllTag();
        if (is_post()) {
            try {
                $data = request()->post();
                $data['siteid'] = request()->nowsiteid;
                $bllTag = new BllTag();
                $bllTag->edit($id, request()->nowsiteid, $data);
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            $tag = $bllTag->find($id, request()->nowsiteid);
            View::assign('tag', $tag);
            return $this->view();
        }
    }

    public function delete()
    {
        try {
            $id = request()->get('id');
            $bllTag = new BllTag();
            $bllTag->delete($id,request()->nowsiteid);
        } catch (Exception $ex) {
            return $this->error($ex);
        }
        return $this->success();
    }
}
